package com.device.controller;


import com.alibaba.fastjson.JSONObject;
import com.device.dataobject.device.DeviceDetail;
import com.device.dataobject.vo.ResultVO;
import com.device.entity.Device;
import com.device.service.DeviceService;
import com.device.util.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/device")
public class DeviceController {
    private final DeviceService deviceService;

    @Autowired
    public DeviceController(DeviceService deviceService) {
        this.deviceService = deviceService;
    }

    @GetMapping
    public ResultVO getAllDevices(Integer page, Integer size) {
        List<Device> devices = deviceService.getAllDevices(page, size);
        return ResultVOUtil.returnSuccess(devices);
    }

    @GetMapping("/{id}")
    public ResultVO getDeviceById(@PathVariable("id") Integer id) {
        Device device = deviceService.getDeviceById(id);
        return ResultVOUtil.returnSuccess(device);
    }

    @PostMapping("/search")
    public ResultVO searchDevices(Integer page, Integer size, @RequestBody JSONObject jsonObject) {
        String deviceName = jsonObject.getString("deviceName");
        List<Device> devices = deviceService.searchDevices(page, size, deviceName);
        return ResultVOUtil.returnSuccess(devices);
    }

    @PostMapping("/item")
    public ResultVO getDevicesByAreaAndDeviceName(@RequestBody JSONObject jsonObject) {
        List<DeviceDetail> deviceDetails = deviceService.getDeviceByAreaAndName(jsonObject);
        return ResultVOUtil.returnSuccess(deviceDetails);
    }

    @GetMapping("/list")
    public ResultVO getDeviceNameList() {

        List<String> deviceNameList = deviceService.getDeviceNameList();
        return ResultVOUtil.returnSuccess(deviceNameList);

    }

    @RequestMapping(value = "/image", method = RequestMethod.POST)
    public ResultVO uploadAvatar(MultipartFile file) {
        return deviceService.uploadImage(file);
    }


}
